package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.LocationMapper;
import com.dk.pojo.Location;
import com.dk.pojo.LocationExample;
import com.dk.pojo.User;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.LocationService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LocationServiceImpl implements LocationService {

    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public PageResult<Location> getLocationInfo(Integer pageNum, Integer pageSize, String name, Integer wid) {
        if(!redisTemplate.hasKey("mesLocation"+name+wid)){
            LocationExample example = new LocationExample();
            if(wid != null && !"".equals(wid)){
                example.createCriteria().andWidEqualTo(wid);
            }
            if( name != null && name.length() > 0){
                example.createCriteria().andNameLike("%"+name+"%");
            }
            List<Location> locations = locationMapper.selectByExample(example);
            redisTemplate.boundValueOps("mesLocation"+name+wid).set(JSONObject.toJSONString(locations));
            return getLocationPageResult(pageNum, pageSize, locations);
        }else {
            String o = (String) redisTemplate.boundValueOps("mesLocation" + name + wid).get();
            List<Location> locations = JSONObject.parseArray(o, Location.class);
            return getLocationPageResult(pageNum, pageSize, locations);
        }

    }

    private PageResult<Location> getLocationPageResult(Integer pageNum, Integer pageSize, List<Location> locations) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Location> pb = new PageInfo<>(locations);
        PageResult<Location> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addLocation(Location location, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            location.setAddPerson(user.getUsername());
            location.setAddDate(new Date());
            locationMapper.insertSelective(location);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateLocation(Location location, String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            location.setUpdatePerson(user.getUsername());
            location.setUpdateDate(new Date());
            locationMapper.updateByPrimaryKeySelective(location);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteLocation(Integer lid) {
        locationMapper.deleteByPrimaryKey(lid);
    }
}